package com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workcorrect;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskClass;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskClassRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkCorrectService;

/**
 * 获取班级用户作业详情
 */
@CrossOrigin
@RestController
public class WorkcorrectFindClassStuTaskController extends YxyApiBaseController {

    @Autowired
    HomeworkCorrectService homeworkCorrectService;

    @Autowired
    HomeworkTaskClassRepository homeworkTaskClassRepository;

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Autowired
    HomeworkTaskReportQuesRepository homeworkTaskReportQuesRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/worklist/workcorrect/stutask/find", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam String taskId, //
            @RequestParam String classId, //
            ReqModel reqModel, //
            RespModel respModel) throws ParseException {
        // ##
        // 获取用户基本信息
        try {
            reqModel.userId = getProfile(request, response).getId();
            reqModel.schoolId = getProfile(request, response).getSchoolId();
        } catch (Exception e) {
            Assert.isTrue(false, "用户信息异常，请重新登录。");
        }
        reqModel.taskId = taskId;
        reqModel.classId = classId;
        // ##
        // 获取任务基本信息
        HomeworkTaskClass homeworkTaskClass = homeworkTaskClassRepository.findTop1ByTaskIdAndClassId(taskId, classId);
        // ##
        // 获取作业班级用户
        List<HomeworkTaskReport> homeworkTaskReports = homeworkTaskReportRepository
                .findByTaskIdAndClassIdOrderByFinishedDateAsc(taskId, classId);
        // 获取有任务的班级学生
        List<String> students = new ArrayList<String>();
        homeworkTaskReports.forEach(homeworkTaskReport -> students.add(homeworkTaskReport.getStudentId()));
        // ##
        // 获取通知类作业
        List<HomeworkTaskReportQues> HomeworkTaskReportQueslist = homeworkTaskReportQuesRepository
                .findByTaskIdAndQuestionTypeAndStudentIdIn(taskId, "2", students);
        // ##
        // 对数据进行处理,转化为 RespModel 对象
        return homeworkCorrectService.convertRespModel(homeworkTaskClass, homeworkTaskReports,
                HomeworkTaskReportQueslist);
    }

    /** 请求参数 **/
    public static class ReqModel extends YxyApiReqModel {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 用户Id
        public String userId;
        // 学校Id
        public String schoolId;
    }

    /** 响应 **/
    public static class RespModel extends YxyApiRespModel {
        // 任务Id
        public String taskId;
        // 任务名称
        public String taskName;
        // 年级
        public String grade;
        // 班级名称
        public String className;
        // 通知类试题
        public List<ClassStuTaskReport> classStuTaskReports;
    }

    public static class ClassStuTaskReport {
        // 任务Id
        public String taskId;
        // 班级Id
        public String classId;
        // 班级名称
        public String className;
        // 作业学生任务试题Id
        public String taskReportQuesId;
        // userId
        public String studentId;
        // userNick
        public String studentNick;
        // user Img
        public String studentImg;
        // 评分
        public Integer mark;
        // 学生是否提交作业
        public Integer statusOfFinished;
        // 学生提交的作业
        public String answerText;

    }
}
